package com.nuoniu.sibanyun.controller.po;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.bo.VoucherDetailBo;
import com.nuoniu.sibanyun.entity.bo.VoucherMainBo;
import com.nuoniu.sibanyun.entity.bo.VoucherSearchBo;
import com.nuoniu.sibanyun.entity.po.VPoPurchaseOrderDetail;
import com.nuoniu.sibanyun.entity.po.VPoPurchaseOrderMain;
import com.nuoniu.sibanyun.entity.po.VRoRequisitionOrderDetail;
import com.nuoniu.sibanyun.entity.po.VRoRequisitionOrderMain;
import com.nuoniu.sibanyun.service.erp.IErpAccountingService;
import com.nuoniu.sibanyun.service.erp.IErpVoucherDesignService;
import com.nuoniu.sibanyun.service.po.IVPoPurchaseOrderMainService;
import com.nuoniu.sibanyun.service.po.IVRoRequisitionOrderDetailService;
import com.nuoniu.sibanyun.service.po.IVRoRequisitionOrderMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 请购单管理
 * @author 小啊刚
 * @since 2020-11-19
 */
@RestController
@RequestMapping("/po/requisitionOrder")
@Api(value="请购单管理", tags={"请购单管理"})
public class VRequisitionOrderController {

    @Autowired
    private IVRoRequisitionOrderMainService requisitionOrderMainService;
    @Autowired
    private IVRoRequisitionOrderDetailService requisitionOrderDetailService;
    @Autowired
    private IVPoPurchaseOrderMainService purchaseOrderMainService;

    @PostMapping("save")
    @ApiOperation("保存请购单")
    public HttpResult save(@RequestBody VRoRequisitionOrderMain requisitionOrderMain) {
        if(requisitionOrderMain.getOrderDetailList() == null || requisitionOrderMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("请购单明细不能为空");
        }
        if(requisitionOrderMain.getId() != null) {
            List<VPoPurchaseOrderMain> vPoPurchaseOrderMains = purchaseOrderMainService.getBySourceCode(requisitionOrderMain.getSourceVoucherCode());
            if(vPoPurchaseOrderMains.size() > 0) {
                return HttpResult.ok("已生成采购单,不能修改");
            }
        }

        requisitionOrderMainService.saveOrder(requisitionOrderMain);
        return HttpResult.ok();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除请购单")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public HttpResult delete(Long id) {
        VRoRequisitionOrderMain requisitionOrderMain = requisitionOrderMainService.getById(id);
        if (requisitionOrderMain == null) {
            throw new NuoNiuException("已删除");
        }
        if(!requisitionOrderMain.getCreateUserId().equals(UserThreadLocal.get().getUserId())) {
            throw new NuoNiuException("你只能删除自己的数据");
        }

        List<VPoPurchaseOrderMain> vPoPurchaseOrderMains = purchaseOrderMainService.getBySourceCode(requisitionOrderMain.getVoucherCode());
        if(vPoPurchaseOrderMains.size() > 0) {
            return HttpResult.ok("已生成采购单,不能删除");
        }

        requisitionOrderMainService.removeById(id);
        requisitionOrderDetailService.removeByOrderId(id);
        return HttpResult.ok("删除成功");
    }


    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        requisitionOrderMainService.submit(id, status);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        requisitionOrderMainService.examine(id,status);
        return HttpResult.ok();
    }


    /**
     * 获取单据编码
     * @author 小啊刚
     * @date 2020-11-27
     */
    @GetMapping("getCode")
    @ApiOperation("获取单据编码")
    public HttpResult getCode() {
        return HttpResult.ok(requisitionOrderMainService.getCode(UserThreadLocal.get().getCompanyId()));
    }


}
